package cn.yunhe.bizImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.yunhe.biz.BookBiz;
import cn.yunhe.dao.BookDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.Book;
import cn.yunhe.util.SqlSessionFactoryUtil;

public class BookBizImpl implements BookBiz {
	SqlSession session=SqlSessionFactoryUtil.getSqlSession();
	BookDao bookDao=(BookDao)session.getMapper(BookDao.class);
	public List<Book> getAllBook() {
		List<Book> bookList=bookDao.getAllBook();
		return bookList;
	}

	public List<Book> getBookByCondition(String param1, String param2) {
		List<Book> bookList=new ArrayList<Book>();
		if("byName".equals(param1)){
			Book book=new Book();
			book.setBookName("%"+param2+"%");
			bookList=bookDao.getBookByCondition(book);
		}else if("byAuthor".equals(param1)){
			Book book=new Book();
			book.setBookAuthor("%"+param2+"%");
			bookList=bookDao.getBookByCondition(book);
		}else if("byPublish".equals(param1)){
			Book book=new Book();
			book.setBookPublish("%"+param2+"%");
			bookList=bookDao.getBookByCondition(book);
		}else if("byCurrCount".equals(param1)){
			Book book=new Book();
			book.setBookCurrCount(Integer.parseInt(param2));
			bookList=bookDao.getBookByCondition(book);
		}
		return bookList;
	}

	public boolean updateBookByBookId(Book book) {
		boolean flag=bookDao.updateBookByBookId(book);
		session.commit();
		return flag;
	}

	public boolean deleteBookByBookId(int bookId) {
		boolean flag=bookDao.deleteBookByBookId(bookId);
		session.commit();
		return flag;
	}

	public Admin loginAdmin(Admin admin) {
		Admin admin1=bookDao.loginAdmin(admin);
		return admin1;
	}

	public Book getBookByBookId(int bookId) {
		Book book=bookDao.getBookByBookId(bookId);
		return book;
	}
	@Test
	public void test1(){
		Book book=new Book();
		book.setBookAuthor("dsklfjds");
		book.setBookName("sdfkljsdkf");
		book.setBookCurrCount(45);
		book.setBookISBN("1234567895");
		book.setBookId(2);
		book.setBookPublish("dkfsdfjsdkljf");
		boolean flag=bookDao.updateBookByBookId(book);
		session.commit();
		System.out.println(flag);
	}

}
